package Classes.GameViews
{
	import Classes.GameViews.ViewDisplayers.MapTargets.MapMoveTargets.MMT_Character;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class Map_hit extends Sprite
	{
		public var mapWidth:Number;
		public var mapHeight:Number;
		public var scalex:Number 	= 1;
		public var scaley:Number	= 1;
		
		public function Map_hit(w:Number,h:Number)
		{
			mapWidth	=	w;
			mapHeight	=	h;
			scalex	=	w/GV_Map.instance.width;
			scaley	=	h/GV_Map.instance.height;
			
			this.addEventListener(MouseEvent.MOUSE_DOWN,mouseDown);
			this.addEventListener(MouseEvent.MOUSE_MOVE,mouseMove);
			this.addEventListener(MouseEvent.MOUSE_UP,mouseUp);
		}
		
		private var isDown:Boolean=false;
		private function mouseDown(e:Event):void
		{
			isDown	=	true;
			
		}
		
		private function mouseMove(e:Event):void
		{
			if(isDown)updateSbar();
		}
		
		private function mouseUp(e:Event):void
		{
			updateSbar();
			isDown = false;
		}
		
		public function updateSbar():void
		{
			var mx:Number	=	this.mouseX;
			var my:Number	=	this.mouseY;
			
			mx	-=	GV_Map.instance.stage.stageWidth*scalex/2;
			my  -=  GV_Map.instance.stage.stageHeight*scaley/2;
			
			if(mx<0)mx=0;
			if(my<0)my=0;
			if( (mx+GV_Map.instance.stage.stageWidth*scalex) > mapWidth)mx = mapWidth-GV_Map.instance.stage.stageWidth*scalex;
			if( (my+GV_Map.instance.stage.stageHeight*scaley) > mapHeight)my = mapHeight-GV_Map.instance.stage.stageHeight*scaley;
			
			GV_Map.instance.x	=	-mx /this.scalex + GV_Map.instance.width/2;
			GV_Map.instance.y	=   -my / this.scaley;
		}
		
		public function reshow():void
		{
			this.graphics.clear();
			this.graphics.beginFill(0x000000);
			this.graphics.drawRect(0, 0, mapWidth, mapHeight);
			this.graphics.endFill();
			
			var	hitsx:Number	=	GV_Map.instance.x-GV_Map.instance.width/2;
			var hitsy:Number	=	GV_Map.instance.y;
//			trace( hitsx*scalex, hitsy*scaley, GV_Map.instance.stage.stageWidth*scalex, GV_Map.instance.stage.stageHeight*scaley, scalex, scaley);
			this.graphics.beginFill(0x0000ff);
			this.graphics.drawRect( -hitsx*scalex, -hitsy*scaley, GV_Map.instance.stage.stageWidth*scalex, GV_Map.instance.stage.stageHeight*scaley);
			this.graphics.endFill();
			
			showMapTarget();
		}
		
		public function showMapTarget():void
		{
			var mx:Number	=	( (GV_Map.instance.controlTarget as MMT_Character).x + GV_Map.instance.width/2)*scalex;
			var my:Number	=	(GV_Map.instance.controlTarget as MMT_Character).y*scaley;
			this.graphics.beginFill(0x00ff00);
			this.graphics.drawRect(mx-2,my-2,2,2);
			//			this.graphics.drawRect(screenPoint.x,screenPoint.y,scale,scale);
			this.graphics.endFill();
		}
		
//		public function reshow():void
//		{
//			this.mapWidth		= 	GV_Map.instance.mapData.widthNum;
//			this.mapHeight		=	GV_Map.instance.mapData.heightNum;
//			
//			this.graphics.clear();
//			this.graphics.beginFill(0x000000);
//			this.graphics.drawRect(0, 0, mapWidth*scale, mapHeight*scale);
//			this.graphics.endFill();
//			
//			var mx:Number	=	GV_Map.instance.controlTarget.mapGrid.atx*scale;
//			var my:Number	=	GV_Map.instance.controlTarget.mapGrid.aty*scale;
////			trace(mx,my, mapWidth, mapHeight);
////			var screenPoint:Point=GU_IsoUtils.isoToScreen(new Point(mx,my));
//			this.graphics.beginFill(0x00ff00);
//			this.graphics.drawRect(mx,my,scale,scale);
////			this.graphics.drawRect(screenPoint.x,screenPoint.y,scale,scale);
//			this.graphics.endFill();
//			
//			var sw:Number	=	GV_Map.instance.stage.stageWidth;
//			var sh:Number	=	GV_Map.instance.stage.stageHeight;
//			var mapw:Number	=	GV_Map.instance.width;
//			var maph:Number =  GV_Map.instance.height;
//			var hitsw:Number	=	mapWidth * sw/mapw;
//			var hitsh:Number	=	mapHeight * sh/maph;
//			
//			this.graphics.beginFill(0x0000ff);
//			this.graphics.drawRect(0,0,hitsw,hitsh);
//			//			this.graphics.drawRect(screenPoint.x,screenPoint.y,scale,scale);
//			this.graphics.endFill();
//		}
		
		/*
		var mmtObj:Object=GV_Map.instance.controlTargetObj;
		for each(var mmtView:MMT_Character in mmtObj)
		{
		mmtView.isControl=false;
		mmtView.controlJianTouVisable(false);
		}
		this.isControl=true;
		GV_Map.instance.controlTarget=this;
		*/
	}
}